home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
028a
/
sq0928.zip
/
UNDOS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-04-27
|
6KB
|
270 lines
/*% cc -xenix -M0 -compat -Osa -K -i % -o undos
*
* Undos - change DOS format files to Unix, etc.
*/
char ID[] =
"Undos Rev 04-25-91 Copyright Omen Technology Inc All Rights Reserved\n";
/*
* This program and documentation may be copied, used, or modified
* by ZCOMM, Professional-YAM and PowerCom licensees provided these notices
* are not removed. Others may use this program for non-profit purposes only.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <utime.h>
#define LL 10240
#define SUB 032
#if 0
struct utimbuf {
time_t actime; /* access time */
time_t modtime; /* modification time */
};
#endif
char Lbuf[LL+2];
char *Progname;
int Todos = 0;
int Tocpm = 0;
int Tomac = 0;
int Unmac = 0;
int Strip = 0;
int Stripsp = 0;
int Graphics = 0;
int Unparity = 0;
int Munged = 0;
int Lineflush = 0; /* Flush output at end of each line */
main(argc, argv)
char **argv;
{
Progname = *argv;
if (! strcmp(Progname, "tocpm"))
Todos = Tocpm = 1;
if (! strcmp(Progname, "todos"))
Todos = 1;
if (! strcmp(Progname, "unmac"))
Unmac = 1;
if (! strcmp(Progname, "tomac"))
Tomac = 1;
if (! strcmp(Progname, "unparity"))
Unparity = 1;
if (! strcmp(argv[1], "-p")) {
++Strip; ++Stripsp; --argc; ++argv;
}
if (! strcmp(argv[1], "-s")) {
++Strip; --argc; ++argv;
}
if (! strcmp(argv[1], "-g")) {
Strip = 0; ++Graphics; --argc; ++argv;
}
if (argc == 1) {
chngfmt(NULL); exit(0);
}
if (argc<2 || *argv[1]== '-')
usage();
while (--argc >= 1)
chngfmt(*++argv);
exit(Munged);
}
usage()
{
fprintf(stderr, ID);
fprintf(stderr, "\nUsage: {undos|tounix|todos|tocpm|unmac|tomac} [-p] [-s] [file ...]\n");
fprintf(stderr, " -p Strip trailing spaces, parity bit, ignore bytes < 007\n");
fprintf(stderr, " -s Strip parity bit, ignore bytes < 007\n");
fprintf(stderr, " -g Allow Graphics (line drawing) characters\n");
fprintf(stderr, "-or- unparity [file ...]\n");
exit(1);
}
chngfmt(name)
char *name;
{
register c;
register char *p;
register n;
register FILE *fin;
FILE *fout;
int linno = 0;
long fpos;
struct stat st, ost;
struct utimbuf times;
char outnam[64];
long ftell();
char *mktemp();
int nlong = LL;
if (name) {
if (stat(name, &st)) {
xperror(name); return;
}
if ((st.st_mode & S_IFMT) != S_IFREG) {
fprintf(stderr, "%s: %s is not a regular file\n", Progname, name);
return;
}
if ((fin = fopen(name, "r")) == NULL) {
xperror(name); return;
}
strcpy(outnam, "undosXXXXXX");
mktemp(outnam);
if ((fout = fopen(outnam, "w")) == NULL) {
xperror(outnam); exit(2);
}
} else {
fin = stdin; fout = stdout;
}
if (fstat(fileno(fout), &ost)) {
xperror("Can't fstat output!"); return;
}
if ((ost.st_mode & S_IFMT) != S_IFREG) {
Lineflush = 1;
}
if (Unparity) {
while ((c = getc(fin)) != EOF)
if (putc((c & 0177), fout) == EOF) {
xperror(outnam); exit(2);
}
goto closeit;
}
for (;;) {
++linno;
Lbuf[0] = 0;
for (p=Lbuf+1, n=LL; --n>0; ) {
ignore:
if ((c = getc(fin)) == EOF)
break;
if ( !c)
goto ignore;
if (c & 0200 && !Graphics) {
if (Strip) {
if ((c &= 0177) < 7)
goto ignore;
} else if (name)
goto thisbin;
}
if (c < '\7') {
if (Strip) {
if ((c &= 0177) < 7)
goto ignore;
} else if (name)
goto thisbin;
}
if (c == SUB) {
if (linno == 1 && name) /* ARC or ZOO file */
goto thisbin;
break;
}
if (c == '\r' && Unmac)
c = '\n';
*p++ = c;
if (c == '\n')
break;
}
*p = '\0';
if (n < nlong)
nlong = n;
if (n == 0 && name) {
thisbin:
if (n) {
fprintf(stderr, "%s: %s is a binary file", Progname, name);
fprintf(stderr, " line=%d char =%2X\n", linno, c);
} else {
fprintf(stderr, "line=%d char =%2X\n", linno, c);
fprintf(stderr, "%s: %s has long line!\n", Progname, name);
if (!Unmac)
fprintf(stderr, "Try unmac?\n");
}
Munged = 1; fclose(fin); fclose(fout);
unlink(outnam); return;
}
if (Todos) {
if (*--p == '\n' && p[-1] != '\r') {
*p++ = '\r'; *p++ = '\n'; *p = 0;
}
} else if (Tomac) {
if (*--p == '\n') {
if (p[-1] == '\r')
--p;
*p++ = '\r'; *p = 0;
}
} else {
if (*--p == '\n' && *--p == '\r') {
while (p>(Lbuf+1) && p[-1] == '\r')
--p;
if (Stripsp)
while (p>(Lbuf+1) && p[-1] == ' ')
--p;
*p++ = '\n'; *p = 0;
}
}
if (Lbuf[1] && fputs(Lbuf+1, fout) == EOF) {
xperror(outnam); exit(2);
}
switch (c) {
case EOF:
if (ferror(fin)) {
xperror(name); exit(3);
}
case SUB:
if (Tocpm) {
fpos = ftell(fout);
do {
putc(SUB, fout);
} while (++fpos & 127);
}
closeit:
if ( !name)
return;
fclose(fout); fclose(fin);
if (st.st_nlink > 1)
sprintf(Lbuf, "cp %s %s", outnam, name);
else
sprintf(Lbuf, "mv %s %s", outnam, name);
system(Lbuf);
times.actime = st.st_atime;
times.modtime = st.st_mtime;
if (utime(name, ×)) {
xperror("Can't set file date");
}
if (st.st_nlink > 1)
unlink(outnam);
nlong = LL - nlong;
if (nlong > 132)
fprintf(stderr, "Longest line in %s has %d bytes.\n",
name ? name:"stdin", nlong);
return;
}
if (Lineflush)
fflush(fout);
}
}
xperror(s)
char *s;
{
register char *p;
extern int sys_nerr;
extern char *sys_errlist[];
extern errno;
if (errno >= sys_nerr)
p = "Gloryovsky: a New Error!";
else
p = sys_errlist[errno];
fprintf(stderr, "%s: %s: %s\n", Progname, s, p);
}